local skels = {}

local shengong = fk.CreateSkill{
    name = "ym1__shengong",
}
Fk:loadTranslationTable{
    ["ym1__shengong"] = "神工",
    [":ym1__shengong"] = "出牌阶段限三次，你可以弃置一张牌并展示一张少于三条<a href=':ym1__shengong_curse'>魔咒</a>的装备牌，从三条随机魔咒中选择一条<a href=':ym1__tianjiang_curse'>附魔</a>之，然后你可将此牌置入其他角色装备区并摸两张牌。",
    ["@[curse]ym1__curse"] = "附魔",
    ["#ym1__shengong-prompt"] = "是否发动 神工，弃置一张牌并为一张装备牌附魔",
    ["#ym1__shengong-curse"] = "神工：请选择一条魔咒",
    ["#ym1__shengong-choose"] = "神工：你可以令一名其他角色装备此牌，然后你摸两张牌",
    ["ym1__shengong_active"] = "神工",
    ["#ym1__shengong-log"] = "%from 发动了〖%arg〗，为 %arg2 附魔了 %arg3",
    ["#ym1__shengong-fail"] = "%arg 已经没有可附魔的魔咒了",

    [":ym1__shengong_curse"] = "魔咒——"..
    "<br><b>锋利</b>：你使用伤害牌无视目标防具。"..
    "<br><b>火焰附加</b>：结束阶段，本回合受到过伤害的角色受到1点火焰伤害。"..
    "<br><b>掠夺</b>：你使用【杀】造成伤害后，获得目标一张牌。"..
    "<br><b>引雷</b>：你使用【杀】时，与目标进行“浮雷”判定。"..
    "<br><b>击退</b>：你造成伤害后，目标获得一个“<a href=':ym1__dun-round'>钝</a>”标记至本轮结束。"..
    "<br><b>保护</b>：此装备进入你的装备区时，你获得1点护甲。"..
    "<br><b>缓冲</b>：你受到大于1点的伤害时，弃置此牌防止之。"..
    "<br><b>荆棘</b>：你受到其他角色的伤害后，弃置伤害来源一张牌。"..
    "<br><b>水下呼吸</b>：你的手牌上限+2。"..
    "<br><b>效率</b>：你使用【桃】或【酒】时，牌面数值+1。"..
    "<br><b>时运</b>：你的判定牌生效前，可以亮出牌堆顶的一张牌代替之。"..
    "<br><b>忠诚</b>：此牌本回合首次离开装备区后，置入你的手牌区。"..
    "<br><b>绑定诅咒</b>：你不能再使用相同副类别的装备牌。"..
    "<br><b>精准采集</b>：每回合首次摸牌时，改为摸四张牌再弃置两张牌。"..
    "<br><b>经验修补</b>：此装备魔咒上限为5，你回复体力时，为此装备再添加1条魔咒。"..
    "<br><b>无限</b>：你因使用失去【杀】时，防止之。"..
    "<br><b>冰霜行者</b>：你抵消牌后，使用者对其自己造成1点冰冻伤害。",
    [":ym1__tianjiang_curse"] = "魔咒适用性——"..
    "<br><b>武器</b>：锋利、火焰附加、掠夺、引雷、击退、效率、精准采集"..
    "<br>※牌名含“弓”的武器：无限（不与经验修补兼容）"..
    "<br><b>防具</b>：保护、缓冲、荆棘、水下呼吸"..
    "<br><b>坐骑</b>：缓冲、冰霜行者"..
    "<br><b>宝物</b>：效率"..
    "<br><b>通用</b>：时运、忠诚、绑定诅咒、经验修补",
}
Fk:addQmlMark{
    name = "curse",
    how_to_show = function(name, value)
        if type(value) == "table" then
            local curses = {}
            for _, str in ipairs(value) do
                if str:startsWith("@@ym1__shengong") then
                    table.insert(curses,str)
                end
            end
            return tostring(#curses)
        else
            return " "
        end
    end,
    qml_path = "packages/utility/qml/DetailBox"
}
shengong:addEffect("active", {
    anim_type = "special",
    prompt = "#ym1__shengong-prompt",
    card_num = 1,
    target_num = 0,
    target_filter = Util.FalseFunc,
    card_filter = function (self, player, to_select, selected, selected_targets)
        return not player:prohibitDiscard(to_select)
    end,
    times = function(self, player)
        return player.phase == Player.Play and 3 - player:usedSkillTimes(shengong.name, Player.HistoryPhase) or -1
    end,
    can_use = function (self, player)
        return player:usedSkillTimes(shengong.name, Player.HistoryPhase) < 3
    end,
    on_use = function (self, room, effect)
        local from = effect.from
        --room:notifySkillInvoked(from,shengong.name,"special")
        --from:broadcastSkillInvoke(shengong.name,table.random({1,2}))
        room:throwCard(effect.cards, shengong.name, from, from)
        if from:isAlive() then
            local equips = table.filter(from:getCardIds("he"),function (id)
                local card = Fk:getCardById(id)
                if card.type == Card.TypeEquip then
                    local mark = {}
                    local max = 3
                    if card:getMark("@@ym1__shengongC6") == 1 then
                        max = 5
                    end
                    for k, v in pairs(card.mark) do
                        if k and k:startsWith("@@ym1__shengong") and v and v ~= 0 then
                            table.insert(mark, k)
                            if #mark >= max then
                                break
                            end
                        end
                    end
                    return #mark < max
                end
            end)
            if #equips == 0 then return end
            local equip = room:askToCards(from, {
                max_num = 1,
                min_num = 1,
                include_equip = true,
                skill_name = shengong.name,
                pattern = tostring(Exppattern{ id = equips })
            })
            from:showCards(equip)
            local curse = {}
            local sub_type = Fk:getCardById(equip[1]).sub_type
            if sub_type == Card.SubtypeWeapon then
                curse = {"@ym1__shengongA1","@ym1__shengongA2","@ym1__shengongA3","@ym1__shengongA4","@ym1__shengongA5",
                "@ym1__shengongC1","@ym1__shengongC2","@ym1__shengongC3","@ym1__shengongC4","@ym1__shengongC5","@ym1__shengongC6"}
                if string.find(Fk:translate(Fk:getCardById(equip[1]).name, "zh_CN"), "弓") ~= nil and 
                Fk:getCardById(equip[1]):getMark("@@ym1__shengongC6") == 0 then
                    table.insert(curse,"@ym1__shengongD1")
                end
            elseif sub_type == Card.SubtypeArmor then
                curse = {"@ym1__shengongB1","@ym1__shengongB2","@ym1__shengongB3","@ym1__shengongB4",
                "@ym1__shengongC2","@ym1__shengongC3","@ym1__shengongC4","@ym1__shengongC6"}
            elseif table.contains({Card.SubtypeOffensiveRide,Card.SubtypeDefensiveRide},sub_type) then
                curse = {"@ym1__shengongB2","@ym1__shengongD2",
                "@ym1__shengongC2","@ym1__shengongC3","@ym1__shengongC4","@ym1__shengongC6"}
            else
                curse = {"@ym1__shengongC1","@ym1__shengongC2","@ym1__shengongC3","@ym1__shengongC4","@ym1__shengongC6"}
            end
            for _, str in ipairs(curse) do
                if Fk:getCardById(equip[1]):getMark("@"..str) == 1  or 
                (str == "@ym1__shengongC6" and Fk:getCardById(equip[1]):getMark("@ym1__shengongD1") == 1) then
                    table.removeOne(curse,str)
                end
            end
            if #curse > 0 then
                local choice = room:askToChoice(from, {
                    choices = table.random(curse,3),
                    skill_name = shengong.name,
                    prompt = "#ym1__shengong-curse",
                    detailed = true,
                })
                if choice ~= "Cancel" then
                    room:setCardMark(Fk:getCardById(equip[1]),"@"..choice,1)
                    room:sendLog{
                        type = "#ym1__shengong-log",
                        from = from.id,
                        arg = shengong.name,
                        arg2 = Fk:getCardById(equip[1]):toLogString(),
                        arg3 = choice,
                        toast = true,
                    }
                    local mark = {}
                    table.filter(from:getCardIds("e"),function (id)
                        local card = Fk:getCardById(id)
                        local curses = {}
                        for k, v in pairs(card.mark) do
                            if k and k:startsWith("@@ym1__shengong") and v and v ~= 0 then
                                table.insert(curses,k)
                            end
                        end
                        if #curses > 0 then
                            table.insert(mark,card.name)
                            table.insertTable(mark,curses)
                        end
                    end)
                    if #mark > 0 then
                        room:setPlayerMark(from,"@[curse]ym1__curse",mark)
                    else
                        room:setPlayerMark(from,"@[curse]ym1__curse",0)
                    end
                    room:setPlayerMark(from, "ym1__shengong_cards", equip)
                    local success, dat = room:askToUseActiveSkill(from, {
                        skill_name = "ym1__shengong_active",
                        prompt = "#ym1__shengong-choose",
                        skip = true,
                    })
                    room:setPlayerMark(from, "ym1__shengong_cards", 0)
                    if success and dat then
                        from:broadcastSkillInvoke(shengong.name,table.random({3,6}))
                        local cardId = dat.cards[1]
                        local to = dat.targets[1]
                        room:moveCardIntoEquip(to, cardId, shengong.name, true, from)
                        from:drawCards(2,shengong.name)
                    end
                    mark = {}
                    table.filter(from:getCardIds("e"),function (id)
                        local card = Fk:getCardById(id)
                        local curses = {}
                        for k, v in pairs(card.mark) do
                            if k and k:startsWith("@@ym1__shengong") and v and v ~= 0 then
                                table.insert(curses,k)
                            end
                        end
                        if #curses > 0 then
                            table.insert(mark,card.name)
                            table.insertTable(mark,curses)
                        end
                    end)
                    if #mark > 0 then
                        room:setPlayerMark(from,"@[curse]ym1__curse",mark)
                    else
                        room:setPlayerMark(from,"@[curse]ym1__curse",0)
                    end
                end
            else
                room:sendLog({
                    type = "#ym1__shengong-fail",
                    from = from.id,
                    arg = Fk:getCardById(equip[1]):toLogString(),
                    toast = true,
                })
            end
        end
    end,
})
table.insert(skels, shengong)

local shengong_active = fk.CreateSkill{
    name = "ym1__shengong_active"
}
shengong_active:addEffect("active", {
    card_num = 1,
    target_num = 1,
    card_filter = function (self, player, to_select, selected, selected_targets)
        return #selected == 0 and table.contains(player:getTableMark("ysch3_shengong_cards"), to_select)
    end,
    target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
        return #selected == 0 and #selected_cards == 1 and to_select ~= player.id
        and to_select:canMoveCardIntoEquip(selected_cards[1], true)
    end,
})
table.insert(skels, shengong_active)

return skels